package com.ljy.db_bean_builder.bean_builder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ljy.db_bean_builder.domain.TableColumn;

/** 
* @author James
* @date 2018年6月19日 
*/
public class BeanBuilderUtil {
	public static void createBeanFile(List<TableColumn> tcList,String beanName,String packageUrl,String filePath) throws IOException {
		File file=checkFile(beanName,filePath);
		StringBuilder sb=new StringBuilder();
		createPackageUrl(sb,packageUrl);
		createImportList(sb,tcList);
		createBody(sb,beanName,tcList);
		PrintWriter pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream(file)));
		pw.append(sb);
		pw.flush();
		pw.close();
	}
	
	public static File checkFile(String beanName,String filePath) throws IOException {
		File file=new File(filePath,beanName+".java");
		if(file.exists()) {
			file.delete();
		}
		file.createNewFile();
		return file;
	}
	
	public static void createPackageUrl(StringBuilder sb,String packageUrl) {
		sb.append("package "+packageUrl+";");
		sb.append("\n\n");
	}
	
	public static void createImportList(StringBuilder sb,List<TableColumn> tcList) {
		Set<String> importList=new HashSet<>();
		tcList.forEach(tc->{
			if(tc.getData_type().toLowerCase().contains("date")) {
				importList.add("import java.util.Date;");
			}
		});
		importList.forEach(i->{
			sb.append(i).append("\n");
		});
		sb.append("\n");
	}
	
	public static void createBody(StringBuilder sb,String beanName,List<TableColumn> tcList) {
		sb.append("public class "+beanName+" {").append("\n");
		Collections.sort(tcList,(TableColumn o1, TableColumn o2)->{
			if(o1.getOrdinal_position()>o2.getOrdinal_position()) {
				return 1;
			}else {
				return -1;
			}
		});
		StringBuilder setAndGetSb=new StringBuilder();
		tcList.forEach(tc->{
			String type=null;
			String dataType=tc.getData_type().toLowerCase();
			if(dataType.contains("bigInt")) {
				type="Long";
			}else if(dataType.contains("int")) {
				type="Integer";
			}else if(dataType.contains("date")) {
				type="Date";
			}else if(dataType.contains("float")||dataType.contains("double")||dataType.contains("decimal")) {
				type="Double";
			}else {
				type="String";
			}
			sb.append("	private "+type+" "+tc.getColumn_name()+";\n\n");
			
			String methodName=tc.getColumn_name().substring(0, 1).toUpperCase()+tc.getColumn_name().substring(1);
			setAndGetSb.append("	public void set"+methodName+"("+type+" "+tc.getColumn_name()+") {\n");
			setAndGetSb.append("		this."+tc.getColumn_name()+"="+tc.getColumn_name()+";\n");
			setAndGetSb.append("	}\n\n");
			setAndGetSb.append("	public "+type+" get"+methodName+"() {\n");
			setAndGetSb.append("		return this."+tc.getColumn_name()+";\n");
			setAndGetSb.append("	}\n\n");
		});
		sb.append(setAndGetSb.toString());
		sb.append("}");
	}
}
